﻿namespace PMS.Client.FrmUserJobPosition
{
    using FastReport;
    using FastReport.Preview;
    using FastReport.Utils;
    using PMS.BLL;
    using PMS.Common;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmReport : Form
    {
        private UserJobPositionBLL bll = new UserJobPositionBLL();
        private Button btnPrint;
        private CheckBox cbIncludeChild;
        private IContainer components = null;
        private Label label1;
        private int OrganID = 0;
        private string OrganName = "";
        private Panel panel1;
        private PreviewControl previewControl1;
        private Report report1 = null;
        private DataTable reportList = null;
        private string reportPath = (Application.StartupPath + @"\ReportTemplates\jobPositionChangeLog.frx");

        public FrmReport(int OrganID, string organName)
        {
            this.OrganID = OrganID;
            this.OrganName = organName;
            this.InitializeComponent();
            this.reportList = this.GetUserChangeLogList(true);
            this.BindReport(this.reportList);
        }

        private void BindReport(DataTable list)
        {
            try
            {
                this.report1 = new Report();
                this.report1.Load(this.reportPath);
                this.report1.Parameters.FindByName("OrganName").Value = this.OrganName;
                this.report1.RegisterData(list, "UserChangeLogList");
                this.report1.Preview = this.previewControl1;
                this.report1.Show();
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                if (MessageBox.Show("报表加载失败，是否重新加载？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    this.BindReport(list);
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                this.report1.FileName = this.OrganName + "职位变动统计表_" + DateTime.Now.ToString("yyyyMMdd");
                this.report1.Print();
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                MessageBox.Show("未安装打印机或打印服务未开启！");
            }
        }

        private void cbIncludeChild_CheckedChanged(object sender, EventArgs e)
        {
            this.reportList = this.GetUserChangeLogList(this.cbIncludeChild.Checked);
            this.BindReport(this.reportList);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private DataTable GetUserChangeLogList(bool isWithChildOrgan)
        {
            DataTable userJobPostion = null;
            if (isWithChildOrgan)
            {
                userJobPostion = this.bll.GetUserJobPostion(PMS.Common.Keys.GetAllOrganIDList(this.OrganID));
            }
            else
            {
                userJobPostion = this.bll.GetUserJobPostion(this.OrganID.ToString());
            }
            if (userJobPostion == null)
            {
                userJobPostion = new DataTable();
            }
            return userJobPostion;
        }

        private void InitializeComponent()
        {
            this.previewControl1 = new PreviewControl();
            this.panel1 = new Panel();
            this.cbIncludeChild = new CheckBox();
            this.btnPrint = new Button();
            this.label1 = new Label();
            this.panel1.SuspendLayout();
            base.SuspendLayout();
            this.previewControl1.BackColor = SystemColors.Control;
            this.previewControl1.Dock = DockStyle.Fill;
            this.previewControl1.Font = new Font("宋体", 9f);
            this.previewControl1.Location = new Point(0, 0);
            this.previewControl1.Margin = new Padding(0);
            this.previewControl1.Name = "previewControl1";
            this.previewControl1.PageOffset = new Point(0, 0);
            this.previewControl1.Size = new Size(950, 0x1b5);
            this.previewControl1.StatusbarVisible = false;
            this.previewControl1.TabIndex = 0;
            this.previewControl1.ToolbarVisible = false;
            this.previewControl1.UIStyle = UIStyle.VisualStudio2005;
            this.panel1.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.panel1.Controls.Add(this.cbIncludeChild);
            this.panel1.Controls.Add(this.btnPrint);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = DockStyle.Bottom;
            this.panel1.Location = new Point(0, 0x1b5);
            this.panel1.Name = "panel1";
            this.panel1.Padding = new Padding(0, 3, 0, 3);
            this.panel1.Size = new Size(950, 30);
            this.panel1.TabIndex = 1;
            this.cbIncludeChild.AutoSize = true;
            this.cbIncludeChild.Checked = true;
            this.cbIncludeChild.CheckState = CheckState.Checked;
            this.cbIncludeChild.Location = new Point(4, 7);
            this.cbIncludeChild.Name = "cbIncludeChild";
            this.cbIncludeChild.Size = new Size(0x5e, 0x10);
            this.cbIncludeChild.TabIndex = 2;
            this.cbIncludeChild.Text = "包括下属机构";
            this.cbIncludeChild.UseVisualStyleBackColor = true;
            this.cbIncludeChild.CheckedChanged += new EventHandler(this.cbIncludeChild_CheckedChanged);
            this.btnPrint.BackColor = Color.White;
            this.btnPrint.Dock = DockStyle.Right;
            this.btnPrint.FlatStyle = FlatStyle.Flat;
            this.btnPrint.Location = new Point(0x35a, 3);
            this.btnPrint.Margin = new Padding(0);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new Size(0x4b, 0x18);
            this.btnPrint.TabIndex = 0;
            this.btnPrint.Text = "打印";
            this.btnPrint.UseVisualStyleBackColor = false;
            this.btnPrint.Click += new EventHandler(this.btnPrint_Click);
            this.label1.Dock = DockStyle.Right;
            this.label1.Location = new Point(0x3a5, 3);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x11, 0x18);
            this.label1.TabIndex = 1;
            this.label1.Text = "  ";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(950, 0x1d3);
            base.Controls.Add(this.previewControl1);
            base.Controls.Add(this.panel1);
            base.Name = "FrmReport";
            this.Text = "FrmReport";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            base.ResumeLayout(false);
        }
    }
}

